package top.withwings.bonus.bonus.repo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import top.withwings.bonus.bonus.pojo.Good;
import top.withwings.bonus.common.pojo.PageParam;

import java.util.List;

@Repository
public class GoodRepo {

    @Autowired private JdbcTemplate jdbcTemplate;
    @Autowired private NamedParameterJdbcTemplate namedJdbcTemplate;


    public Good findById(int goodId){
        String sql = " select id ,code, name,specification,standard_price,enabled from good where id = ?";
        try{
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Good.class),goodId);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }


    public List<Good> search(PageParam pageParam, String keyword, boolean enabledOnly){
        String sql = "  select id ,code, name,specification,standard_price,enabled from good where 1=1 ";

        if(enabledOnly){
            sql +=" and enabled=1 ";
        }

        if(!StringUtils.isEmpty(keyword)){
            sql+=" and (name like :keyword or code like :keyword)";
        }

        sql += " limit  :start ,:length";

        MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource();
        sqlParameterSource.addValue("start",pageParam.getStart())
                    .addValue("length",pageParam.getLength())
                    .addValue("keyword","%"+keyword+"%");
        return namedJdbcTemplate.query(sql, sqlParameterSource, new BeanPropertyRowMapper<>(Good.class));

    }

    public int count(String keyword, boolean enabledOnly){
        String sql = "  select count(*) from good where 1=1 ";

        if(enabledOnly){
            sql +=" and enabled=1 ";
        }

        if(!StringUtils.isEmpty(keyword)){
            sql+=" and (name like :keyword or code like :keyword)";
        }

        MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource();
        sqlParameterSource.addValue("keyword","%"+keyword+"%");
        return namedJdbcTemplate.queryForObject(sql,sqlParameterSource,Integer.class);
    }


    public int create(Good good){
        KeyHolder keyHolder = new GeneratedKeyHolder();
        String sql = " insert into "
                + " good "
                +" (code, "
                +" name, "
                +" specification, "
                +" standard_price, "
                +" enabled "
                +" )values( "
                +" :code, "
                + " :name, "
                + " :specification, "
                +" :standardPrice, "
                +" :enabled) ";

        namedJdbcTemplate.update(sql , new BeanPropertySqlParameterSource(good),keyHolder);
        return keyHolder.getKey().intValue();



    }

    public void update(Good good){
        String sql = " update "
                +" good "
                +" set "
                +" code = :code, "
                +" name = :name, "
                +" specification = :specification, "
                +" standard_price = :standardPrice, "
                +" enabled = :enabled "
                +" where  "
                +" id = :id ";
        namedJdbcTemplate.update(sql,new BeanPropertySqlParameterSource(good));



    }

    public void delete(int goodId){
        String sql = "delete from good where id = ?";
        jdbcTemplate.update(sql,goodId);

    }


}
